﻿using System.Data;
using LazyFieldDemo.Models;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data;
using Orchard.Data.Migration;

namespace LazyFieldDemo.Migrations {
    public class CrmMigrations : DataMigrationImpl {
        private readonly IRepository<Country> _countryRepository;
        public CrmMigrations(IRepository<Country> countryRepository) {
            _countryRepository = countryRepository;
        }

        public int Create() {
             SchemaBuilder.CreateTable("Country", table => table
                 .Column<int>("Id", c => c.Identity().PrimaryKey())
                 .Column<string>("Code", c => c.WithLength(3).WithType(DbType.StringFixedLength))
                 .Column<string>("Name", c => c.WithLength(50)));

             SchemaBuilder.CreateTable("AddressPartRecord", table => table
                 .ContentPartRecord()
                 .Column<string>("AddressLine1", c => c.WithLength(256))
                 .Column<string>("AddressLine2", c => c.WithLength(256))
                 .Column<string>("Zipcode", c => c.WithLength(20))
                 .Column<string>("City", c => c.WithLength(50))
                 .Column<int>("Country_Id", c => c.Nullable()));

             SchemaBuilder.CreateTable("CustomerPartRecord", table => table
                 .ContentPartRecord()
                 .Column<string>("FirstName", c => c.WithLength(50))
                 .Column<string>("LastName", c => c.WithLength(50))
                 .Column<int>("ShippingAddressId", c => c.Nullable())
                 .Column<int>("BillingAddressId", c => c.Nullable()));

             ContentDefinitionManager.AlterPartDefinition("AddressPart", part => part.Attachable(false));
             ContentDefinitionManager.AlterPartDefinition("CustomerPart", part => part.Attachable(false));

             ContentDefinitionManager.AlterTypeDefinition("Address", type => type
                 .WithPart("CommonPart")
                 .WithPart("AddressPart")
                 .Draftable(false)
                 .Creatable());

             ContentDefinitionManager.AlterTypeDefinition("Customer", type => type
                 .WithPart("CommonPart")
                 .WithPart("CustomerPart")
                 .Draftable(false)
                 .Creatable());

             _countryRepository.Create(new Country { Name = "United States", Code = "USA"});
             _countryRepository.Create(new Country { Name = "France", Code = "FRA" });
             _countryRepository.Create(new Country { Name = "Belgium", Code = "BEL" });
             _countryRepository.Create(new Country { Name = "Germany", Code = "DEU" });
             _countryRepository.Create(new Country { Name = "Netherlands", Code = "NLD" });
             _countryRepository.Create(new Country { Name = "Poland", Code = "POL" });
             _countryRepository.Create(new Country { Name = "Spain", Code = "ESP" });
             _countryRepository.Create(new Country { Name = "Hungary", Code = "HUN" });
             _countryRepository.Create(new Country { Name = "Canada", Code = "CAN" });
             _countryRepository.Create(new Country { Name = "Croatia", Code = "HRV" });
             _countryRepository.Create(new Country { Name = "Russian Federation", Code = "RUS" });
             _countryRepository.Create(new Country { Name = "Mozambique", Code = "MOZ" });

             return 1;
         }
    }
}